//
//  RMMCommonAlertView.m
//  Romomo
//
//  Created by puman on 2016/10/27.
//  Copyright © 2016年 mazegong. All rights reserved.
//

#import "RMMCommonAlertView.h"

static NSMutableArray *array;

@implementation RMMCommonAlertView
+ (instancetype)commonAlertViewWithFrame:(CGRect)frame byArray:(NSArray *)titleArray{
    RMMCommonAlertView *alertView = [[self alloc] initWithFrame:frame];
    alertView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(0, alertView.height - 49 * (titleArray.count + 1) - 6 - (titleArray.count - 1) * 1, alertView.width, 49 * (titleArray.count + 1) + 6 + (titleArray.count - 1) * 1)];
    backView.backgroundColor = COLORFROMCODE(0xEEEEEE, 1);
    [alertView addSubview:backView];
    array = [NSMutableArray arrayWithArray:titleArray];
    [array addObject:@"取消"];
    for (int i = 0; i < array.count; i++) {
        UIButton *button = [UIButton buttonWithType:(UIButtonTypeCustom)];
        button.backgroundColor = kWhiteColor;
        button.titleLabel.font = kFont(16);
        [button setTitle:array[i] forState:(UIControlStateNormal)];
        [button addTarget:alertView action:@selector(buttonClick:) forControlEvents:(UIControlEventTouchUpInside)];
        if (i < array.count - 1) {
            button.frame = CGRectMake(0, kHalf * i, backView.width, 49);
            [button setTitleColor:COLORFROMCODE(0x2C2C2C, 1) forState:(UIControlStateNormal)];
        } else {
            button.frame = CGRectMake(0, backView.height - 49, backView.width, 49);
            [button setTitleColor:COLORFROMCODE(0x9B9B9B, 1) forState:(UIControlStateNormal)];
        }
        [backView addSubview:button];
    }
    return alertView;
}
- (void)buttonClick:(UIButton *)button{
    NSString *string =  [button  titleForState:(UIControlStateNormal)];
    for (int i = 0; i < array.count; i++) {
        if ([string isEqualToString:array[i]]) {
            if ([self.delegate respondsToSelector:@selector(commonAlertView:didSelectItemAtIndex:)]) {
                [self.delegate commonAlertView:self didSelectItemAtIndex:i];
                [UIView animateWithDuration:kInterval animations:^{
                    self.frame =  CGRectMake(0, kScreenHeight, kScreenWideth, kScreenHeight);
                } completion:^(BOOL finished) {
                    
                }];
            }
        }
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
